/*
	Title: 02_Table8.do
	Paper: Brauning, Ivashina, Ozdagli (2025)
	Description: Table 8 - The effect of the COVID-19 shock on (latent) covenant violation
	Note: Uses Version 5 of reghdfe
*/

/*------------------------------------------------------------------------------
								  Table 8
------------------------------------------------------------------------------*/
// Import Table 8 data
use "$data_path/Table8.dta", clear 


// Table Setup -----------------------------------------------------------------
putdocx clear 
putdocx begin
putdocx paragraph
putdocx table table8 = (20, 6), border(insideV, nil) border(insideH, nil) border(left, nil) border(right, nil)

* Left margin
putdocx table table8(6,1) = ("Incurrence bind")
putdocx table table8(8,1) = ("Maintenance bind")
putdocx table table8(11,1) = ("Observations")
putdocx table table8(12,1) = ("R-squared")
putdocx table table8(13,1) = ("Firm FE")
putdocx table table8(14,1) = ("Quarter FE")
putdocx table table8(15,1) = ("Controls")
putdocx table table8(16,1) = ("Controls*Cov-type")
putdocx table table8(17,1) = ("Quarter*Cov-type FE")
putdocx table table8(19,1) = ("H0: Incurrence=Maintenance")
putdocx table table8(20,1) = ("p-value")

* Top margin
putdocx table table8(1,2), colspan(6)
putdocx table table8(1,2) = ("Investment/Capital (%)")

putdocx table table8(2,2), colspan(2)
putdocx table table8(2,2) = ("Firm sample split")
putdocx table table8(2,2/3), border(bottom)
putdocx table table8(3,2) = ("Not impacted")
putdocx table table8(3,3) = ("impacted")

putdocx table table8(2,4), colspan(2)
putdocx table table8(2,4) = ("Time sample split")
putdocx table table8(2,4/6), border(bottom)
putdocx table table8(3,5) = ("Excl. COVID period")
putdocx table table8(3,6) = ("COVID period")

putdocx table table8(3,.), border(bottom)

putdocx table table8(4,2) = ("(1)")
putdocx table table8(4,3) = ("(2)")
putdocx table table8(4,5) = ("(3)")
putdocx table table8(4,6) = ("(4)")
putdocx table table8(4,.), border(bottom)


// Define Regression Specifications --------------------------------------------
local LHS "capex_capital_ratio_tr"

local controls0   "L_macroq_tr L_logatq_tr cashflowq_tr" 
local dd_controls " L_DD_Total_final_tr L_DD_Total_final_sq_tr"
local controls    "`controls0' `dd_controls'"

local conditions "L.dealscan <= 2 & _b1_diff_std <= 2 & _b2_diff_std <= 2" 

local cluster = "xdim qdate"


// Column (1) ------------------------------------------------------------------
// Regression:
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & covid_industry == 0, ///
			  version(5) nowarn ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)

// Store results 
matrix reg_results = r(table)
local b1 : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local se1 : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local p = reg_results["pvalue", "L_Iin_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars1 = ""
} 
if (`p' < 0.1) {
	local p_sigstars1 = "*"
}
if (`p' < 0.05) {
	local p_sigstars1 = "**"
}
if (`p' < 0.01) {
	local p_sigstars1 = "***"
}

local b2 : display %04.2f reg_results["b", "L_Min_covenant_violation"]  
local se2 : display %04.2f reg_results["se", "L_Min_covenant_violation"]	
local p = reg_results["pvalue", "L_Min_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars2 = ""
} 
if (`p' < 0.1) {
	local p_sigstars2 = "*"
}
if (`p' < 0.05) {
	local p_sigstars2 = "**"
}
if (`p' < 0.01) {
	local p_sigstars2 = "***"
}

local N = e(N)
local r2 : display %9.2f  e(r2)

lincom L_Min_covenant_violation - L_Iin_covenant_violation
local lincom = r(estimate)
local lincom_p: display %9.4f r(p)

// Put results into table in Word Document 
putdocx table table8(6,2) = ("`b1'`p_sigstars1'")
putdocx table table8(7,2) = ("(`se1')")
putdocx table table8(8,2) = ("`b2'`p_sigstars2'")
putdocx table table8(9,2) = ("(`se2')")
putdocx table table8(11,2) = (`N')
putdocx table table8(12,2) = (`r2')
putdocx table table8(13,2) = ("Yes")
putdocx table table8(14,2) = ("Yes")
putdocx table table8(15,2) = ("Yes")
putdocx table table8(16,2) = ("Yes")
putdocx table table8(17,2) = ("Yes")
putdocx table table8(19,2) = (`lincom')
putdocx table table8(20,2) = (`lincom_p')

			
// Column (2) ------------------------------------------------------------------
// Regression:	
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & covid_industry == 1, ///
			  version(5) nowarn ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)
			  
// Store results 
matrix reg_results = r(table)
local b1 : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local se1 : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local p = reg_results["pvalue", "L_Iin_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars1 = ""
} 
if (`p' < 0.1) {
	local p_sigstars1 = "*"
}
if (`p' < 0.05) {
	local p_sigstars1 = "**"
}
if (`p' < 0.01) {
	local p_sigstars1 = "***"
}

local b2 : display %04.2f reg_results["b", "L_Min_covenant_violation"]  
local se2 : display %04.2f reg_results["se", "L_Min_covenant_violation"]	
local p = reg_results["pvalue", "L_Min_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars2 = ""
} 
if (`p' < 0.1) {
	local p_sigstars2 = "*"
}
if (`p' < 0.05) {
	local p_sigstars2 = "**"
}
if (`p' < 0.01) {
	local p_sigstars2 = "***"
}

local N = e(N)
local r2 : display %9.2f  e(r2)

lincom L_Min_covenant_violation - L_Iin_covenant_violation
local lincom = r(estimate)
local lincom_p: display %9.4f r(p)

// Put results into table in Word Document 
putdocx table table8(6,3) = ("`b1'`p_sigstars1'")
putdocx table table8(7,3) = ("(`se1')")
putdocx table table8(8,3) = ("`b2'`p_sigstars2'")
putdocx table table8(9,3) = ("(`se2')")
putdocx table table8(11,3) = (`N')
putdocx table table8(12,3) = (`r2')
putdocx table table8(13,3) = ("Yes")
putdocx table table8(14,3) = ("Yes")
putdocx table table8(15,3) = ("Yes")
putdocx table table8(16,3) = ("Yes")
putdocx table table8(17,3) = ("Yes")
putdocx table table8(19,3) = (`lincom')
putdocx table table8(20,3) = (`lincom_p')
			
			
// Column (3) ------------------------------------------------------------------
// Regression:	
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & covid_period == 0, ///
			  version(5) nowarn ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)

// Store results 
matrix reg_results = r(table)
local b1 : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local se1 : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local p = reg_results["pvalue", "L_Iin_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars1 = ""
} 
if (`p' < 0.1) {
	local p_sigstars1 = "*"
}
if (`p' < 0.05) {
	local p_sigstars1 = "**"
}
if (`p' < 0.01) {
	local p_sigstars1 = "***"
}

local b2 : display %04.2f reg_results["b", "L_Min_covenant_violation"]  
local se2 : display %04.2f reg_results["se", "L_Min_covenant_violation"]	
local p = reg_results["pvalue", "L_Min_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars2 = ""
} 
if (`p' < 0.1) {
	local p_sigstars2 = "*"
}
if (`p' < 0.05) {
	local p_sigstars2 = "**"
}
if (`p' < 0.01) {
	local p_sigstars2 = "***"
}

local N = e(N)
local r2 : display %9.2f  e(r2)

lincom L_Min_covenant_violation - L_Iin_covenant_violation
local lincom = r(estimate)
local lincom_p: display %9.4f r(p)

// Put results into table in Word Document 
putdocx table table8(6,5) = ("`b1'`p_sigstars1'")
putdocx table table8(7,5) = ("(`se1')")
putdocx table table8(8,5) = ("`b2'`p_sigstars2'")
putdocx table table8(9,5) = ("(`se2')")
putdocx table table8(11,5) = (`N')
putdocx table table8(12,5) = (`r2')
putdocx table table8(13,5) = ("Yes")
putdocx table table8(14,5) = ("Yes")
putdocx table table8(15,5) = ("Yes")
putdocx table table8(16,5) = ("Yes")
putdocx table table8(17,5) = ("Yes")
putdocx table table8(19,5) = (`lincom')
putdocx table table8(20,5) = (`lincom_p')
			  
			  
// Column (4) ------------------------------------------------------------------
// Regression:		
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & covid_period == 1, ///
			  version(5) nowarn ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)		

// Store results 
matrix reg_results = r(table)
local b1 : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local se1 : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local p = reg_results["pvalue", "L_Iin_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars1 = ""
} 
if (`p' < 0.1) {
	local p_sigstars1 = "*"
}
if (`p' < 0.05) {
	local p_sigstars1 = "**"
}
if (`p' < 0.01) {
	local p_sigstars1 = "***"
}

local b2 : display %04.2f reg_results["b", "L_Min_covenant_violation"]  
local se2 : display %04.2f reg_results["se", "L_Min_covenant_violation"]	
local p = reg_results["pvalue", "L_Min_covenant_violation"]
if (`p' > 0.1) {
	local p_sigstars2 = ""
} 
if (`p' < 0.1) {
	local p_sigstars2 = "*"
}
if (`p' < 0.05) {
	local p_sigstars2 = "**"
}
if (`p' < 0.01) {
	local p_sigstars2 = "***"
}

local N = e(N)
local r2 : display %9.2f  e(r2)

lincom L_Min_covenant_violation - L_Iin_covenant_violation
local lincom = r(estimate)
local lincom_p: display %9.4f r(p)

// Put results into table in Word Document 
putdocx table table8(6,6) = ("`b1'`p_sigstars1'")
putdocx table table8(7,6) = ("(`se1')")
putdocx table table8(8,6) = ("`b2'`p_sigstars2'")
putdocx table table8(9,6) = ("(`se2')")
putdocx table table8(11,6) = (`N')
putdocx table table8(12,6) = (`r2')
putdocx table table8(13,6) = ("Yes")
putdocx table table8(14,6) = ("Yes")
putdocx table table8(15,6) = ("Yes")
putdocx table table8(16,6) = ("Yes")
putdocx table table8(17,6) = ("Yes")
putdocx table table8(19,6) = (`lincom')
putdocx table table8(20,6) = (`lincom_p')
			  
	
// Table Setup (end) -----------------------------------------------------------
putdocx table table8(1/20.,2/6), halign(center)
putdocx table table8(.,1), width(3 in)

* Number formatting
putdocx table table8(11,2/6), nformat(%9.0fc)
putdocx table table8(12,2/6), nformat(%9.3g)
putdocx table table8(19,2/6), nformat(%04.3f)
putdocx table table8(20,2/6), nformat(%9.4g)

* Table formatting
putdocx table table8(.,.), font("Times New Roman", 9)

// Export Table 8
putdocx save "$tables_path/Table8.docx", replace

	